package com.dhwl.hsh.merchant.presenter;

import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.resp.AreaResp;
import com.dhwl.hsh.lib.bean.resp.HtmlUrlResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.core.mvp.MvpView;
import com.dhwl.hsh.lib.utils.DateUtil;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.lib.utils.logger.Logger;
import com.dhwl.hsh.merchant.model.impl.CommonModel;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * doc
 *
 * @author hjd
 * @version V1.0.0
 * @Date 16/6/1 下午5:04
 */
public class MainPresenter extends BasePresenter<MvpView> {

    private ReservoirUtils reservoirUtils;

    public MainPresenter() {
        this.reservoirUtils = new ReservoirUtils();
    }

    public void getArea() {
        Subscriber<HttpResponse<List<AreaResp>>> subscriber = new Subscriber<HttpResponse<List<AreaResp>>>() {
            @Override
            public void onCompleted() {
                if (MainPresenter.this.mCompositeSubscription != null)
                    MainPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Logger.e("getArea", e.getMessage());
                HshException exception = new HshException(e.hashCode(), e.getMessage());
            }

            @Override
            public void onNext(HttpResponse<List<AreaResp>> response) {

                if (response.getStatus() != 0) {
                    HshException exception = new HshException(response.getStatus(), response.getDescribe());
                } else {
                    List<AreaResp> areaResp = response.getData();
                    reservoirUtils.refresh("key_area", areaResp);
                    reservoirUtils.refresh("key_area_time", DateUtil.getCurrentTime());
                    if (getMvpView() != null) {
                        getMvpView().onSuccess(areaResp);
                    }
                }
            }
        };

        try {

            Type resultType = new TypeToken<List<AreaResp>>() {
            }.getType();
            List<AreaResp> areaResp = reservoirUtils.getSync("key_area", resultType);

            if (null == areaResp) {
                this.mCompositeSubscription.add(CommonModel.getInstance().getArea().subscribeOn(Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
            } else if (DateUtil.getDifferTime(DateUtil.getCurrentTime(), reservoirUtils.getSync("key_area_time", String.class)) > 15) {
                this.mCompositeSubscription.add(CommonModel.getInstance().getArea().subscribeOn(Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void getHtmlPage() {
        Subscriber<HttpResponse<HtmlUrlResp>> subscriber = new Subscriber<HttpResponse<HtmlUrlResp>>() {
            @Override
            public void onCompleted() {
                if (MainPresenter.this.mCompositeSubscription != null)
                    MainPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Logger.e("getHtmlPage", e.getMessage());
                HshException exception = new HshException(e.hashCode(), e.getMessage());
            }

            @Override
            public void onNext(HttpResponse<HtmlUrlResp> response) {

                if (response.getStatus() != 0) {
                    HshException exception = new HshException(response.getStatus(), response.getDescribe());
                } else {
                    HtmlUrlResp htmlUrlResp = response.getData();

                    reservoirUtils.refresh("key_html", response);
                    reservoirUtils.refresh("key_html_time", DateUtil.getCurrentTime());
                    if (getMvpView() != null) {
                        getMvpView().onSuccess(htmlUrlResp);
                    }

                }
            }
        };

        try {
            HtmlUrlResp htmlUrlResp = reservoirUtils.getSync("key_html", HtmlUrlResp.class);
            if (null == htmlUrlResp) {
                this.mCompositeSubscription.add(CommonModel.getInstance().getHtmlUrl().subscribeOn(Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
            } else if (DateUtil.getDifferTime(DateUtil.getCurrentTime(), reservoirUtils.getSync("key_html_time", String.class)) > 1) {
                this.mCompositeSubscription.add(CommonModel.getInstance().getHtmlUrl().subscribeOn(Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}
